package com.education.wisdom.controller.management;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.config.WebSocketServer;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.DateUtil;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.LessonMenu;
import com.education.wisdom.pojo.LessonResources;
import com.education.wisdom.pojo.Message;
import com.education.wisdom.service.LessonMenuService;
import com.education.wisdom.service.LessonResourcesService;
import com.education.wisdom.service.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 备课资源
 */
@RestController
@RequestMapping("/Manage/Resource")
public class LessonResourcesController {


    @Autowired
    private LessonResourcesService lessonResourcesService;

    @Autowired
    private LessonMenuService lessonMenuService;

    @Autowired
    private UserFeign userFeign;

    @Autowired
    private MessageService messageService;

    /**
     * 查询
     */
    @GetMapping("/SelectList")
    public ApiReturnObject selectList(LessonResources lessonResources, Integer userId, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        lessonResources.setDel("1");
        List<LessonResources> resourcesList = lessonResourcesService.selectLessonResourcesList(lessonResources);
        getCatalog(userId, resourcesList);
        return new ApiReturnObject("200", "操作成功", new PageInfo<LessonResources>(resourcesList));
    }


    private void getCatalog(Integer userId, List<LessonResources> resourcesList) {
        for (LessonResources resources : resourcesList) {
            if (resources.getLessonId() != null) {
                LessonMenu lessonMenu = lessonMenuService.selectLessonMenuById(resources.getLessonId());
                resources.setLesson(lessonMenu.getName());//专业
            }
            if (resources.getRCreateuser() != null) {
                Map<String, Object> map = userFeign.selectByUserId(resources.getRCreateuser());
                String name = "匿名", image = "4.0defaultavatar.png";
                if (null != map && map.size() > 0 && StringUtils.isNotBlank(map.get("name").toString())) {
                    name = map.get("name").toString();
                    image = map.get("image").toString();
                }
                resources.setTeacherName(name);//老师姓名
                resources.setTeacherImage(image);//老师姓名
                Integer count = lessonResourcesService.selectCountByUserId(resources.getRCreateuser());
                resources.setCount(count);//老师上传课件总数
            }
            if (resources.getImage() != null && !"".equals(resources.getImage())) {
                String[] split = resources.getImage().split(",");
                resources.setPreviewArray(split);
            }
        }
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public ApiReturnObject deleteById(String ids) {
        LessonResources lessonResources = new LessonResources();
        lessonResources.setId(Integer.parseInt(ids));
        lessonResources.setDel("0");
        lessonResourcesService.updateLessonResources(lessonResources);
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public ApiReturnObject update(@RequestBody LessonResources lessonResources) {
        lessonResourcesService.updateLessonResources(lessonResources);
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }

    /**
     * 添加
     *
     * @param lessonResources
     * @return
     */
    @PostMapping("/ins")
    public ApiReturnObject ins(@RequestBody LessonResources lessonResources) {
        lessonResources.setRUploadtime(DateUtil.getCurrentDateTime());
        lessonResourcesService.insertLessonResources(lessonResources);
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }


    //资源下线
    @PostMapping("/offline")
    public ApiReturnObject offline(@RequestBody LessonResources lessonResources) {
        lessonResources.setDelFlag("0");//下线资源;
        int i = lessonResourcesService.updateLessonResources(lessonResources);
        LessonResources resources = lessonResourcesService.selectLessonResourcesById(lessonResources.getId());
        Message message = new Message();
        StringBuilder builder = new StringBuilder();
        message.setTitle(builder.append("课程下线通知 : 您在备课共享资源中发布的课程名称为 ").append(resources.getRName()).append(" 的课程违反平台规定, 已被下线处理").toString());
        message.setType("3");
        message.setCreatetime(new Date());
        message.setUserId(resources.getRCreateuser());
        message.setIsRead("1");
        messageService.insertMessage(message);
        //查询消息推送
        Integer count = messageService.selectCount(resources.getRCreateuser().toString());
        try {
            WebSocketServer.sendInfo(count.toString(), resources.getRCreateuser().toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (i > 0) {
            return new ApiReturnObject("200", "操作成功", "操作成功");
        }
        return new ApiReturnObject("201", "操作失败", "操作失败");
    }

    //资源审核
    @PostMapping("/examine")
    public ApiReturnObject examine(@RequestBody LessonResources lessonResources) {
        lessonResourcesService.updateLessonResources(lessonResources);
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }


}